<!-- /* ############ BEGIN ADD STATS ############## */ -->

<?php

require_once  __DIR__ . '/includes/authenticate.php';

if (isset($_POST['season']) && isset($_POST['selectedgame'])) {

$selectedgame = $_POST['selectedgame'];
$season = $_POST['season'];

include_once './db_bb_connect.php';

?>

<Script>
  function setGame(){
  document.getElementById("selected-game").innerHTML = "Game <?= $selectedgame; ?> is selected";
}
  setGame();

</Script>

<div class="container-fluid">
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-6"><h3 class="text-center"> Add stats for <?= $_POST['season']; ?> game <?= $_POST['selectedgame']; ?> </h3></div>
<div class="col-md-3"></div>
</div>
</div>
<br>

<div class="container-fluid">
<div class="row">
<div class="col-md-4 col-lg-4"></div>
<div class="col-md-4 col-lg-4">

<form method="post" action="">
    <div class="form-group">        
      <div class="col-lg-offset-3 col-md-offset-3 col-lg-6 col-md-6">
        <button type="reset" class="btn btn-lg btn-danger btn-block"> RESET FORM</button> 
      </div>
      <br><br>
    </div>
    <div class="form-group">
        <select class="form-control" name="playerID">
          <option value="" disabled="disabled" selected="selected">Select a player</option>
          <option value="team">Team</option>

<?php

$sql = "SELECT users.user_key, users.firstname, users.type, users.lastname, teams.playernumber
FROM users 
JOIN teams
ON users.user_key = teams.playerID
WHERE users.type = 'Player'
AND teams.season = '$season'
ORDER BY teams.playernumber";

$result=$db->query($sql);

while($row=$result->fetch_assoc()){ 

  $user_key=$row['user_key']; 
  $lastname=$row['lastname']; 
  $firstname=$row['firstname']; 
  $type=$row['type']; 
  $playernumber=$row['playernumber'];

  echo "<option value='$user_key'>$playernumber - $firstname $lastname</option>";

$sql2 = "SELECT *
FROM stats 
WHERE stats.playerID = '$user_key'
AND stats.gamenumber = '$selectedgame'
AND stats.season = '$season'";

$sql3 = "SELECT *
FROM users
WHERE EXISTS
(SELECT * FROM stats 
  WHERE stats.playerID = '$user_key' 
  AND stats.gamenumber = '$selectedgame' 
  AND stats.season = '$season' )"; 
}

?>
        </select>
    </div>

      <div class="input-group">
        <input class="form-control" id="ex1" type="number" placeholder="Defensive Rebounds" name="DRB">
       <span class="input-group-addon">DRB</span>
      </div>
      <br/>
      <div class="input-group">
        <input class="form-control" id="ex2" type="number" placeholder="Offensive Rebounds" name="ORB">
        <span class="input-group-addon">ORB</span>
      </div>
      <br/>
     <div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="Turnovers" name="TOV">
        <span class="input-group-addon">TO</span>
      </div>
      <br/>
      <div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="Steals" name="ST">
        <span class="input-group-addon">ST</span>
      </div>
      <br/>

<?php

if ($season >= '2018/2019') {
  
?>

<div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="Assists" name="AST">
        <span class="input-group-addon">AST</span>
      </div>
      <br/>

      <div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="Blocked Shots" name="BS">
        <span class="input-group-addon">BS</span>
      </div>
      <br/>

<?php } 

if ($season >= '2017/2018') {
    $spantextscored = "FGI scored";
    $placeholderscored = "FG Inside scored";
    $spantexttotal = "FGI total";
    $placeholdertotal = "FG Inside total shots";
  } else {
    $spantextscored = "FG scored";
    $placeholderscored = "Field Goals scored";
    $spantexttotal = "FG total";
    $placeholdertotal = "Field Goals total shots";
  }
?>     
      <div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="<?= $placeholderscored; ?>" name="FGscored">
        <span class="input-group-addon"><?= $spantextscored; ?>
          
        </span>
      </div>
      <br/>
      <div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="<?= $placeholdertotal; ?>" name="FGtotal">
        <span class="input-group-addon"><?= $spantexttotal; ?>
        </span>
      </div>
      <br/>

<?php

if ($season >= '2017/2018') {
  
?>

<div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="FG Outside scored" name="FGoutsidescored">
        <span class="input-group-addon">FGO scored</span>
</div>
<br/>

<div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="FG Outside total shots" name="FGoutsidetotal">
        <span class="input-group-addon">FGO total</span>
</div>
<br/>

<div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="3P scored" name="THPscored">
        <span class="input-group-addon">3P scored</span>
</div>
<br/>

<div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="3P total shots" name="THPtotal">
        <span class="input-group-addon">3P total</span>
</div>
<br/>

<div class="input-group">
  <input class="form-control" id="ex3" type="number" placeholder="Free Throws scored" name="FTscored">
  <span class="input-group-addon">FT scored</span>
</div>
<br/>
<div class="input-group">
 <input class="form-control" id="ex3" type="number" placeholder="Free Throws total attempts" name="FTtotal">
   <span class="input-group-addon">FT total</span>
</div>
<br/>

<?php
} ?>

 <div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="Personal Fouls" name="PF">
        <span class="input-group-addon">PF</span>
      </div>
      <br/>

<?php

if ($season >= '2018/2019') {

?>

<div class="input-group">
        <input class="form-control" id="ex3" type="text" placeholder="Minutes Played 00:00" name="MP">
        <span class="input-group-addon">MP</span>
</div>
<br/>


<div class="form-group">
  <label for="ex3">Starting 5:</label>
        <select class="form-control" name="S5">
          <option value="yes">yes</option>
          <option value="no" selected="selected">no</option>
        </select>
</div>
<br/>

<?php
}

?>
   
      <div class="form-group">
        <label for="ex3">DNP</label>
        <select name="DNP" class="form-control">
          <option value="no" selected="selected">no</option>
          <option value="yes">DNP</option>
          <option value="coach"> DNP coach</option>
          <option value="injury">DNP injury</option>
        </select>
      </div>

       <div id="addstatshandler3content"> </div>

    <input type="hidden" name="season" value="<?= $_POST['season']; ?>"/>
    <input type="hidden" name="selectedgame" value="<?= $_POST['selectedgame']; ?>"/>
    <br><br>
    <div class="form-group">        
      <div class="col-lg-offset-4 col-md-offset-4 col-lg-4 col-md-4">
        <button type="submit" class="btn btn-lg btn-success btn-block" id="addstatssubmit"> Add</button>
      </div>
    </div>
   </form>

    

</div>

<div class="col-md-4 col-lg-4"></div>
</div>
</div>
   <br>
   <br>

<?php 

 }

 ?>

<script type="text/javascript">
$("#addstatssubmit").on("click", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
      type: "POST",
      url: "./includes/handlers/addstats_bytotals_handler.php",
      data: postData,
      success: function(data) {
        $("#addstatshandler3content").html(data);
      }
  }); 
  e.preventDefault();
});


</script>
